<template>
  <div class="login-container">
    <!-- 导航栏 -->
    <van-nav-bar
      class="van-nav-bar"
      title="登录"
      left-text="返回"
      @click-left="$router.back()"
    />
    <!-- 导航栏 -->
    <!-- 表单区域 -->
    <van-form @submit="onSubmit" ref="loginFormRef">
      <van-field
        name="mobile"
        placeholder="请输入手机号"
        v-model="user.mobile"
        :rules="userFormRules.mobile"
        type="number"
        maxlength="11"
      >
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field
        name="code"
        placeholder="请输入验证码"
        v-model="user.code"
        :rules="userFormRules.code"
        type="number"
        maxlength="6"
      >
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <template #button>
          <van-count-down
            :time="1000 * 4"
            format="ss 秒"
            @finish="isCountDownShow = false"
            v-if="isCountDownShow"
          />
          <van-button
            class="send-yam-btn"
            round
            size="small"
            type="default"
            native-type="button"
            @click="onSendSm"
            v-else
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">登录</van-button>
      </div>
    </van-form>
    <!-- 表单区域 -->
  </div>
</template>

<script>
import { login, sendSms } from '../../api/user'
export default {
  name: 'LoginIndex',

  data() {
    return {
      // 用户登录请求对象
      user: {
        // 手机号
        mobile: '13911111111',
        // 验证码
        code: '246810'
      },
      // 用户表单验证对象规则
      userFormRules: {
        mobile: [{
          required: true,
          message: '手机号不能为空！'
        },
        {
          pattern: /^1[3|5|7|8]\d{9}$/,
          message: '请填写正确的手机号！'
        }
        ],
        code: [{
          required: true,
          message: '验证码不能为空！'
        },
        {
          pattern: /^\d{6}$/,
          message: '验证码格式错误！'
        }]
      },
      isCountDownShow: false
    }
  },

  mounted() {
  },

  methods: {
    // 登录表单提交事件
    async onSubmit() {
      this.$toast.loading({
        message: '加载中...',
        forbidClick: true,
        duration: 0
      })
      try {
        const { data } = await login(this.user)
        this.$store.commit('setUser', data.data)
        this.$toast.success('登录成功!')
        this.$router.back()
      } catch (err) {
        console.log(err.response)
        if (err.response.status === 400) {
          this.$toast.fail('手机或验证码错误!')
        } else {
          this.$toast.fail('登录失败，请稍后重试!')
        }
      }
    },
    // 发送验证码事件
    async onSendSm() {
      // 1.验证手机号
      try {
        await this.$refs.loginFormRef.validate('mobile')
      } catch (err) {
        return console.log('验证失败', err)
      }
      // 2.验证通过，显示倒计时
      this.isCountDownShow = true
      // 3.请求验证码
      try {
        await sendSms(this.user.mobile)
        this.$toast('发送成功!')
      } catch (err) {
        // 发送失败
        this.isCountDownShow = false
        if (err.response.status === 429) {
          this.$toast('发送太频繁了，请稍后重试！')
        } else {
          this.$toast('发送失败!，请稍后重试！')
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  .toutiao {
    font-size: 37px;
  }

  .send-yam-btn {
    background-color: #0069db;
    color: #fff;
  }
}
</style>